package net.nampham.visioncontrol.model;

public class Ray2 extends Line2 {
	Vec2 origin;
	Vec2 dest;
	public Ray2(Vec2 origin, Vec2 dest){
		super(origin, dest);
		this.origin = origin;
		this.dest = dest;
	}
	
	public Ray2(Vec2 origin, Angle angle){
		this(origin, origin.add(
				new Vec2((float)Math.sin(angle.val), 
						(float)Math.cos(angle.val))));
	}
	
	public Vec2 Intersect(Line2 other){
		Vec2 ret = super.Intersect(other);
		if (ret!=null){
			//check whether ret is on the same side of the ray
			if ((dest.x - origin.x) * (ret.x-origin.x)>0) return ret;
			if ((dest.y - origin.y) * (ret.y-origin.y)>0) return ret;			
		}
		return null;
	}
	
	public Vec2 Intersect(Segment2 other){
		Vec2 ret = this.Intersect((Line2)other);
		if (ret!=null){
			//check whether ret is inside the segment 'other'
			if (!other.is_inside(ret)) return null;			
			/*render intersections
			if (CONFIG._DEBUGGER!=null){
				CONFIG._DEBUGGER.renderLine(other.p1, other.p2, DColor.YELLOW);
				CONFIG._DEBUGGER.renderPoint(ret, DColor.YELLOW);
			}
			//*/
		}
		return ret;
	}
}
